package com.longcai.service.impl.trad;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.longcai.bean.ApiResult;
import com.longcai.bean.PageHelper;
import com.longcai.entity.nearby.ClyjTradingArea;
import com.longcai.mapper.trad.ClyjTradingAreaMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longcai.page.table.PageTableResponse;
import com.longcai.service.trad.ClyjTradingAreaService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 商圈表 服务实现类
 * </p>
 *
 * @author M
 * @since 2019-04-11
 */
@Service
public class ClyjTradingAreaServiceImpl extends ServiceImpl<ClyjTradingAreaMapper, ClyjTradingArea> implements ClyjTradingAreaService {


    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ClyjTradingAreaMapper clyjTradingAreaMapper;

    /**
     * 获取商圈列表
     *
     * @param clyjTradingArea
     * @param pageHelper
     * @return
     */
    @Override
    public PageTableResponse tradList(ClyjTradingArea clyjTradingArea, PageHelper pageHelper) {
        //分页
        Page<ClyjTradingArea> page = new Page<ClyjTradingArea>(pageHelper.getStart(), pageHelper.getLength());
        QueryWrapper<ClyjTradingArea> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(clyjTradingArea.getName())) {
            queryWrapper.like("name", clyjTradingArea.getName());
        }
        //查询状态为0的头条
        queryWrapper.eq("status", 0);
        IPage<ClyjTradingArea> sysRoleIPage = this.clyjTradingAreaMapper.selectPage(page, queryWrapper);
        PageTableResponse pageTableResponse = new PageTableResponse(sysRoleIPage.getTotal(),
                sysRoleIPage.getTotal(), sysRoleIPage.getRecords());
        return pageTableResponse;
    }

    /**
     * 根据名称查询商圈信息
     *
     * @param name
     * @return
     */
    @Override
    public ClyjTradingArea getName(String name) {
        try {
            QueryWrapper<ClyjTradingArea> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name", name);
            ClyjTradingArea clyjTradingArea = this.clyjTradingAreaMapper.selectOne(queryWrapper);
            logger.debug("查询商圈{}", clyjTradingArea);
            return clyjTradingArea;
        } catch (Exception e) {
            logger.error("查询失败");
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 新增商圈
     *
     * @param clyjTradingArea
     * @return
     */
    @Override
    public ClyjTradingArea saveHead(ClyjTradingArea clyjTradingArea) {
        try {
            ClyjTradingArea clyjTradingArea1 = clyjTradingArea;
            clyjTradingArea1.setId(UUID.randomUUID().toString().replace("-", ""));
            clyjTradingArea1.setStatus(0);
            clyjTradingArea1.setCreateTime(new Date());
            Integer integer = this.clyjTradingAreaMapper.insert(clyjTradingArea1);
            logger.debug("新增成功++++++++++++", integer);
            return clyjTradingArea1;
        } catch (Exception e) {
            logger.error("新增失败");
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 根据id获取商圈信息
     *
     * @param id
     * @return
     */
    @Override
    public ApiResult getById(String id) {

        try {
            ClyjTradingArea clyjTradingArea = this.clyjTradingAreaMapper.selectById(id);
            logger.debug("查询成功++++++++++++++", clyjTradingArea);
            return ApiResult.success(clyjTradingArea);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 修改商圈信息
     *
     * @param clyjTradingArea
     * @return
     */
    @Override
    public Integer updateHead(ClyjTradingArea clyjTradingArea) {

        try {
            clyjTradingArea.setUpdateTime(new Date());
            return this.clyjTradingAreaMapper.updateById(clyjTradingArea);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 删除商圈
     *
     * @param id
     */
    @Override
    public void deleteById(String id) {
        try {
            QueryWrapper<ClyjTradingArea> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", id);
            ClyjTradingArea clyjTradingArea = new ClyjTradingArea();
            //修改状态为1
            clyjTradingArea.setStatus(1);
            Integer integer = this.clyjTradingAreaMapper.update(clyjTradingArea, queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
